package game.entities;

import game.Game;
import game.InputHandler;
import game.gfx.Colors;
import game.gfx.Font;
import game.gfx.Screen;
import game.level.Level;
import game.level.tiles.Tile;

public class Rogue extends Hostile {

	private InputHandler input;
	private int color = Colors.get(-1, 111, 145, 543); // (-1, 111, 333, 543)
														// for sword
	private int scale = 1;
	protected boolean isSwimming = false;
	private int tickCount = 0;
	private int classType;
	private String username;
	private static boolean heldJump = false;
	private long lastTime = System.currentTimeMillis();
	private long thisTime = System.currentTimeMillis();
	private boolean dir = true;

	public Rogue(Level level, int x, int y, int fallSpdForDmg, Player target) { // need
																				// to
																				// add
																				// a
																				// health
																				// attribute
																				// here.
		super(level, "Rogue", x, y, 1, target, 2);
		this.username = "Rogue";
		this.health = 200;
		this.maxHealth = 200;
		this.classType = 0;
		this.fallSpdForDmg = fallSpdForDmg;
	}

	public void tick() {
		int xa = 0;
		int ya = 0;
		// if(xa != 0 || ya != 0){
		if (!seesTarget(target)) {
			idle();
		} else {
			attack(target);
		}
		if (level.getTile(this.x / 8, this.y / 8).getId() == 3) {
			isSwimming = true;
		}
		if (isSwimming && level.getTile(this.x / 8, this.y / 8).getId() != 3) {
			isSwimming = false;
		}
		Tile lastTile = level.getTile((this.x + 7) >> 3, (this.y + 7) >> 3);
		if ((isSolidTile(1, 0, 5, 7) || isSolidTile(1, 0, 6, 7)
				|| isSolidTile(-1, 0, 2, 7) || isSolidTile(-1, 0, 1, 7) || lastTile
					.isSolid())
				&& !isSolidTile(0, 1, 5, 7)
				&& !isSolidTile(-1, 0, 1, 3) && !isSolidTile(1, 0, 6, 3)) {
			y--;
		}
		tickCount++;
	}

	public void render(Screen screen) {
		int xTile = 0;
		int yTile = 22;
		int walkingSpeed = 4;
		int flipTop;
		int flipBottom;
		if (dir) {
			flipTop = 0;
			flipBottom = 0;
		} else {
			flipTop = 1;
			flipBottom = 1;
		}
		if (movingDir == 1) {
			xTile = 0;
		} else if (movingDir > 1) {
			xTile += 4 + ((numSteps >> walkingSpeed) & 1) * 2;
			flipTop = (movingDir - 1) % 2;
		}
		int modifier = 8 * scale;
		int xOffset = x - modifier / 2;
		int yOffset = y - modifier / 2 - 4;
		/*
		 * if (isSwimming) { int waterColor = 0; yOffset += 4; if (tickCount %
		 * 60 < 15) { waterColor = Colors.get(-1, -1, 225, -1); } else if (15 <=
		 * tickCount % 60 && tickCount % 60 < 30) { yOffset -= 1; waterColor =
		 * Colors.get(-1, 225, 115, -1); } else if (30 <= tickCount % 60 &&
		 * tickCount % 60 < 45) { waterColor = Colors.get(-1, 115, -1, 225); }
		 * else { yOffset -= 1; waterColor = Colors.get(-1, 225, 115, -1); }
		 * screen.render(xOffset, yOffset + 3, 0 + 27 * 32, waterColor, 0x00,
		 * 1); screen.render(xOffset + 8, yOffset + 3, 0 + 27 * 32, waterColor,
		 * 0x01, 1);}
		 */
		screen.render(xOffset + modifier - (modifier * flipTop), yOffset, xTile
				+ 1 + yTile * 32, color, flipTop, scale);
		screen.render(xOffset + (modifier * flipTop), yOffset, (xTile) + yTile
				* 32, color, flipTop, scale);

		// Heart rendering
		if (health > 0) {
			if ((double) (maxHealth / health) >= 1
					&& (double) (maxHealth / health) < 2) {
				screen.render(xOffset + 4, yOffset + 17, 31 + 29 * 32,
						Colors.get(-1, 300, 500, 555), 0x00, scale);
			} else if ((double) (maxHealth / health) >= 2
					&& (double) (maxHealth / health) < 3) {
				screen.render(xOffset + 4, yOffset + 17, 31 + 29 * 32,
						Colors.get(-1, 200, 400, 444), 0x00, scale);
			} else if ((double) (maxHealth / health) >= 3
					&& (double) (maxHealth / health) < 4) {
				screen.render(xOffset + 4, yOffset + 17, 31 + 29 * 32,
						Colors.get(-1, 100, 300, 333), 0x00, scale);
			} else {
				screen.render(xOffset + 4, yOffset + 17, 31 + 29 * 32,
						Colors.get(-1, 000, 200, 222), 0x00, scale);
			}
		}

		if (!isSwimming) {
			screen.render(xOffset + modifier - (modifier * flipBottom), yOffset
					+ modifier, xTile + 1 + (yTile + 1) * 32, color,
					flipBottom, scale);
			screen.render(xOffset + (modifier * flipBottom),
					yOffset + modifier, (xTile) + (yTile + 1) * 32, color,
					flipBottom, scale);
		}
		if (name != null) {
			String usernamehp = name + " - " + health;
			Font.render(usernamehp, screen, xOffset - (usernamehp.length() - 1)
					/ 2 * 8, yOffset - 10, Colors.get(-1, -1, -1, 555), 1);
		}
	}

	public boolean hasCollided(int xa, int ya) {
		int xMin = 0;
		int xMax = 7;
		int yMin = -7;
		int yMax = 7;
		for (int x = xMin; x < xMax; x++) {
			if (isSolidTile(xa, ya, x, yMin)) {
				return true;
			}
		}
		for (int x = xMin; x < xMax; x++) {
			if (isSolidTile(xa, ya, x, yMax)) {
				return true;
			}
		}
		for (int y = yMin; y < yMax; y++) {
			if (isSolidTile(xa, ya, xMin, y)) {
				return true;
			}
		}
		for (int y = yMin; y < yMax; y++) {
			if (isSolidTile(xa, ya, xMax, y)) {
				return true;
			}
		}
		return false;
	}

	public String getUsername() {
		return this.name;
	}

	public static boolean getHeldJump() {
		return heldJump;
	}

	public void idle() {
		int xa = 0;
		int ya = 0;
		thisTime = System.currentTimeMillis();
		if (thisTime - lastTime >= 30) {
			if ((isSolidTile(0, 1, 8, 7) || isSolidTile(0, 1, 5, 7)) && dir
					&& !isSolidTile(1, 0, 8, -7)) {
				xa = 0;
			} else {
				this.dir = false;
			}
			if ((isSolidTile(0, 1, 0, 7) || isSolidTile(0, 1, 5, 7)) && !dir
					&& !isSolidTile(-1, 0, 0, -7)) {
				xa = 0;
			} else {
				this.dir = true;
			}
			move(xa, ya);
			lastTime = thisTime;
		} else {
			move(0, 0);
		}
	}

	public void attack(Player target) {
		move(0, 0);
	}

	public boolean seesTarget(Player player) {
		return false;
	}
}
